const router = require('koa-router')()
const upload = require('../upload')
const mongoose = require('mongoose')
router.prefix('/images')

router.post('/', upload.single('imgfile'),async function(ctx){
  const {title, description} = ctx.request.body
  const {originalname,mimetype,filename:diskfilename} = ctx.req.file
  const doc = await Image.create({
    title,description,originalname,mimetype,diskfilename
  })
  ctx.redirect(`/images/${dov._id}`)
})

router.get('/:imageid',async ctx =>{
  const doc =await Image.findById(ctx.params.imageid)
  if(doc){
      await ctx.render('show',doc)
  }
})
module.exports = router